package org.jcommon.com.util.collections;

import java.util.*;

import org.jcommon.com.util.system.SystemListener;
/**
 * 
 * @author LeoLee<leo.li@protel.com.hk>
 *
 */
public class MapStore implements SystemListener{
	private Map<Object, Object> store;
	
	public MapStore(){
		store = Collections.synchronizedMap(new HashMap<Object,Object>());
	}
	
	public MapStore(Map<Object, Object> store){
		this.store = store;
	}
	
	protected boolean addOne(Object key, Object value){
		synchronized(store){
			if(!store.containsKey(key)){
				store.put(key, value);
				return true;
			}
		}
		return false;
	}
	
	protected boolean hasKey(Object key){
		return store.containsKey(key);
	}
	
	protected boolean updateOne(Object key, Object value){
		synchronized(store){
			if(store.containsKey(key)){
				store.put(key, value);
				return true;
			}
		}
		return false;
	}
	
	protected Object getOne(Object key){
		if(key==null)return null;
		synchronized(store){
			if(store.containsKey(key))
				return store.get(key);
		}
		return null;
	}
	
	protected boolean removeOne(Object key){
		synchronized(store){
			if(store.containsKey(key)){
				store.remove(key);
				return true;
			}
		}
		return false;
	}
	
	protected Map<Object, Object> getAll(){
		return store;
	}
	
	public void clear() {
		// TODO Auto-generated method stub
		store.clear();
	}

	@Override
	public void shutdown() {
		// TODO Auto-generated method stub
		clear();
	}

	@Override
	public void startup() {
		// TODO Auto-generated method stub
		
	}
}
